package com.example.administrator.baikangxing.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.administrator.baikangxing.bean.SleepBean;
import com.example.administrator.baikangxing.utils.ScreenUtils;

import java.util.List;


/**
 * Created by manbuAndroid5 on 2016/10/14.
 */

public class SleepTimeView extends View {

    private Paint clearPaint;
    private Paint lightPaint;
    private Paint deepPaint;

    private int clearColor = 0xFFFF8500;
    private int lightColor = 0xFF8AEA86;
    private int deepColor = 0xFF086D03;

    private int strokeWidth = 2;

    private RectF mBounds;
    private float width;
    private float height;
    private float selfHeight;

    private float totalTime = 7;
    private float perHourWidth;
    private List<SleepBean> sleepBeanList;

    public SleepTimeView(Context context) {
        super(context);
        init();
        selfHeight = ScreenUtils.dip2px(context,120);
    }

    public SleepTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        selfHeight = ScreenUtils.dip2px(context,120);
    }

    public SleepTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        selfHeight = ScreenUtils.dip2px(context,120);
    }

    private void init(){
        clearPaint = new Paint();
        clearPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        clearPaint.setStrokeWidth(strokeWidth);
        clearPaint.setColor(clearColor);
        clearPaint.setAntiAlias(true);
        clearPaint.setDither(true);

        lightPaint = new Paint();
        lightPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        lightPaint.setStrokeWidth(strokeWidth);
        lightPaint.setColor(lightColor);
        lightPaint.setAntiAlias(true);
        lightPaint.setDither(true);

        deepPaint = new Paint();
        deepPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        deepPaint.setStrokeWidth(strokeWidth);
        deepPaint.setColor(deepColor);
        deepPaint.setAntiAlias(true);
        deepPaint.setDither(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBounds = new RectF(getLeft(),0,getRight(),200);
        width = mBounds.right - mBounds.left;
        height = mBounds.bottom - mBounds.top;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xffffff);
        perHourWidth = width / totalTime;
        int sizeOfSleep = sleepBeanList == null ? 0 : sleepBeanList.size();
        Log.e("lym","w:"+width+"perW:"+perHourWidth+"size:"+sizeOfSleep+"height:"+height);
        RectF rectf = null;
        float starX = 0;
        int type = 0;
        float sleepTime = 0;
        float showWidth = 0;
        for (int i = 0; i < sizeOfSleep; i++) {
            type = sleepBeanList.get(i).getSleepType();
            sleepTime = sleepBeanList.get(i).getSleepTime();
            showWidth = sleepTime * perHourWidth;
            if (type == 0){//清醒
                rectf = new RectF(starX,50,starX+showWidth,selfHeight);
                starX += showWidth+2;
                canvas.drawRect(rectf,clearPaint);
            }else if (type == 1){//浅睡
                rectf = new RectF(starX,50,starX+showWidth,selfHeight);
                starX += showWidth+2;
                canvas.drawRect(rectf,lightPaint);
            }else {//深睡
                rectf = new RectF(starX,0,starX+showWidth,selfHeight);
                starX += showWidth+2;
                canvas.drawRect(rectf,deepPaint);
            }
            Log.e("lym","type:"+type+"sleepTime:"+sleepTime+"showWidth:"+showWidth);
        }

    }

    public void setResources(List<SleepBean> list,float totalTime){
        this.sleepBeanList = list;
        this.totalTime = totalTime;
        invalidate();
    }
}
